using System.Text;
using LuckyLang.AST.Expression;

namespace LuckyLang;

public class VariateManager
{
    public Dictionary<string,Value> Variates         { get; set; } = new (); // va/C

    public List<Value> ClassAndFuncInfo { get; set; } = new List<Value>();

    public Value Result { get; set; }

    public List<int> Chirdren { get; set; } = new List<int>();

    public override string ToString()
    {
        StringBuilder builder = new StringBuilder();
        foreach (KeyValuePair<string,Value> variate in Variates)
            builder.Append(variate.Key+" : "+variate.Value+"\n");
        return builder.ToString();
    }
    public void Post(ID id,Value value)
    {
        var a = Variates.Keys;
        if (a.Any(x => id.IdName == x))
            Variates[id.IdName] = value;
        else
            Variates.Add(id.IdName,value);
    }
    public Value Get(ID id)
    {
        var a = Variates.Keys;
        if (a.Any(x => id.IdName == x))
            return Variates[id.IdName];
        return null;
    }
    public void Add() => Chirdren.Add(Variates.Count-1);
    public void Remove()
    {
        int a = Chirdren.Last(); 
        Chirdren.RemoveAt(Chirdren.Count -1);
        var b = Variates.Keys.ToArray();
        for (int i = Variates.Count -1; i > a; i++)
        {
            var c = b[i];
            Variates.Remove(c);
        }
    }
}